loadstring(exports.dgs:dgsImportFunction())()

cmtaSTACJAPALIW = false
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
local number = 0
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 17*px )
tick = getTickCount()

playerData={}

local blips={
{-1674.69873, 413.95731, 6.84192},
{-2026.08093, 155.35880, 30.30973},
{-2411.38794, 973.50311, 47.20167},
{-1471.74243, 1864.39099, 31.85349},
{2201.83667, 2475.38623, 10.48534},
{2639.37793, 1107.31555, 10.48567},
{2115.74463, 919.66266, 10.48560},
{1381.67249, 458.42462, 19.64229},
{653.81982, -565.17767, 16.00063},
{-90.60886, -1168.40320, 2.08376},
{1942.37451, -1772.68652, 13.64062},
{-1605.62073, -2714.55811, 48.53347},
{-2244.17676, -2561.30029, 31.92188},
{613.15326, 1693.01770, 6.99219},
{2147.38428, 2747.84351, 10.82031},
{-1328.31677, 2677.22144, 50.06250},
{1596.14758, 2202.24097, 10.82031},
}

local petrolStations = {
    {-1477.38, 1857.36, 32.64},
    {-1329.21, 2672.04, 50.06},
    {-1328.23, 2677.46, 50.06},
    {-1327.43, 2682.90, 50.06},
    {605.18, 1704.82, 6.99},
    {608.57, 1699.80, 6.99},
    {611.88, 1694.71, 6.99},
    {615.36, 1689.92, 6.99},
    {618.76, 1684.96, 6.99},
    {622.39, 1680.03, 6.99},
    {2147.60, 2745.02, 10.82},
    {2147.52, 2750.73, 10.82},
    {2114.63, 916.66, 10.82},
    {2114.94, 923.13, 10.82},
    {2199.15, 2474.44, 10.82},
    {2205.34, 2474.62, 10.82},
    {1383.79, 461.56, 20.12},
    {1380.98, 457.75, 19.94},
    {653.21, -559.70, 16.34},
    {653.18, -570.31, 16.34},
    {-99.38, -1173.08, 2.42},
    {-93.96, -1161.43, 2.22},
    {-1608.85, -2718.85, 48.54},
    {-1605.79, -2714.38, 48.53},
    {-1602.33, -2709.97, 48.54},
    {-2244.03, -2561.04, 31.92},
    {-2407.94, 970.53, 45.30},
    {-2408.06, 981.22, 45.30},
    {-1667.44, 410.55, 7.18},
    {-1671.93, 414.67, 7.18},
    {-1674.42, 417.12, 7.18},
    {-1678.42, 421.17, 7.18},
    {-2024.10, 156.98, 28.84},
    {-2029.36, 156.56, 28.84},
    {1944.16, -1772.85, 13.39},
    {1938.89, -1772.77, 13.38},
    {2639.86, 1103.39, 10.82},
    {2639.91, 1109.84, 10.82},
    {1596.18811, 2191.26758, 10.82031},
    {1596.14758, 2202.24097, 10.82031},

}


local petrolMathematic = {

    [25] = {
        multiplier = 4;
    };
    [35] = {
        multiplier = 2,857142857142857;
    };
    [50] = {
        multiplier = 2;
    };

}

for i,v in ipairs(petrolStations) do
	local marker=createMarker(v[1], v[2], v[3], "cylinder", 2, 255, 100, 0,0)
    setElementData(marker, 'marker:data', {name='Dystrybutor', icon='stations',size=1})
	setElementData(marker,"cmta_marker:station",true)

end

for i,v in ipairs(blips) do
    createBlip(v[1],v[2],v[3],56)

end

addEventHandler("onClientMarkerHit",getRootElement(),function(hit)
    if hit ~= localPlayer then return end
    if not isPedInVehicle(hit) then return end
    if getVehicleController(getPedOccupiedVehicle(localPlayer)) ~= localPlayer then return end
    if not getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").id then return end
    if getElementData(source,"cmta_marker:station") then
        cmtaSTACJAPALIW = true
        rndRect = dgsCreateRoundRect(30,false,tocolor(0,0,0,190))  
        rect1 = dgsCreateImage(400*px,320*py,566*px,338*py,rndRect,false)
        progress = dgsCreateProgressBar(100*px,330*py,368*px,-368*py,false,rect1,false,tocolor(255,217,217),false,tocolor(210,105,30),false) 
        playerData[localPlayer] = 0
        dgsProgressBarSetStyle(progress,"ring-round",{
            rotation = -90})
        dgsSetProperty(progress,"indicatorColor",tocolor(50,255,20))

        dgsProgressBarSetProgress(progress,tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").fuel)*petrolMathematic[getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").tank].multiplier)


        text = dgsCreateLabel(0*px,10*py,566*px,50*py,"#ffffffWitaj na stacji benzynowej",false,rect1)
        dgsSetFont( text, font2 )
        dgsSetProperty(text,"colorcoded",true)
        dgsSetProperty(text,"alignment",{"center","center"})

        text1 = dgsCreateLabel(0*px,260*py,566*px,50*py,"Aby zatankować pojazd przytrzymaj #ccc000SPACJĘ\n#ffffffDo zapłaty: #ff0000"..playerData[localPlayer].." pkt\n#ffffffCena: #ff00005pkt/dm³",false,rect1)
        dgsSetFont( text1, font )
        dgsSetProperty(text1,"colorcoded",true)
        dgsSetProperty(text1,"alignment",{"center","center"})

        text2 = dgsCreateLabel(0*px,125*py,566*px,50*py,"#00ff00"..(string.format("%.01f",tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").fuel))).."/"..(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").tank).."",false,rect1)
        dgsSetFont( text2, font2 )
        dgsSetProperty(text2,"colorcoded",true)
        dgsSetProperty(text2,"alignment",{"center","center"})

        addEventHandler("onClientRender",root,bind)

    end
end)

addEventHandler("onClientMarkerLeave",getRootElement(),function(hit)
    if hit ~= localPlayer then return end
    if getElementData(source,"cmta_marker:station") then
    if cmtaSTACJAPALIW then
        cmtaSTACJAPALIW = false
        destroyElement(rndRect)
        destroyElement(rect1)
        removeEventHandler("onClientRender",root,bind)
        if playerData[localPlayer] ~= 0 then
            triggerServerEvent("cmta_stacjepaliw:kup", resourceRoot,playerData[localPlayer])
            playerData[localPlayer] = 0
        end
    end
end
end)



function bind()
	local v = getPedOccupiedVehicle(localPlayer)
    if v then
	local tank = getElementData(v,"vehicle:data").tank
    local fuel = tonumber(getElementData(v,"vehicle:data").fuel)
    if getElementData(v,"vehicle:data").id then
  	if getKeyState("space") and getTickCount()-tick > 500 then
	if tank == 25 and fuel >=25.00 then return end
	if tank == 35 and fuel >=35.00 then return end
	if tank == 50 and fuel >=50.00 then return end
    dgsProgressBarSetProgress(progress,fuel*petrolMathematic[(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").tank)].multiplier)
    dgsSetText(text1,"Aby zatankować pojazd przytrzymaj #ccc000SPACJĘ\n#ffffffDo zapłaty: #ff0000"..playerData[localPlayer].." pkt\n#ffffffCena: #ff00005pkt/dm³")
    dgsSetText(text2,"#00ff00"..(string.format("%.01f",fuel)).."/"..tank.."")
    local data = getElementData(v,"vehicle:data")
    data.fuel = data.fuel+0.01
    setElementData(v,"vehicle:data",data)
    playerData[localPlayer] = playerData[localPlayer]+0.05
    end
	end
	end
end
--[[
addEventHandler( "onClientPlayerQuit", getRootElement(), function()
    if playerData[source] ~= 0 then
    setElementData(getPedOccupiedVehicle(source),"vehicle:fuel",0)
    end
end)]]